package tree

import "fmt"

func (node *TreeNode) Traverse() {
	node.TraverseFunc(func(n *TreeNode) {
		n.Print()
	})
	fmt.Println()
}

func (node *TreeNode) TraverseFunc(f func(*TreeNode)) {
	if node == nil {
		return
	}

	node.Left.TraverseFunc(f)
	f(node)
	node.Right.TraverseFunc(f)
}

func (node *TreeNode) TraverseWithChannel() chan *TreeNode {
	out := make(chan *TreeNode)
	go func() {
		node.TraverseFunc(func(n *TreeNode) {
			out <- n
		})
		close(out)
	}()
	return out
}
